import axios from 'axios'
// import qs from 'qs'
// import Router from '../router/router'
import {
  Message
} from 'iview'
import theBaseUrl from './baseUrl'
Message.config({
    top: 200,
    duration: 2
  })
  // 创建axios实例 application/x-www-data-urlencoded  application/json
let Axios = axios.create({
  timeout: 600000,
  responseType: "json",
  // withCredentials: true, // 是否允许带cookie这些
  headers: {
    // "Content-Type": "application/x-www-form-urlencoded;charset=utf-8",
    "Content-Type": "application/json-rpc" //"application/json;charset=utf-8"
      // 'Access-Control-Max-Age': '0',
  }
});

if (process.env.NODE_ENV == 'development') {
  Axios.defaults.baseURL = theBaseUrl; //192.168.1.239  120.27.21.149 192.168.3.244
} else if (process.env.NODE_ENV == 'debug') {
  Axios.defaults.baseURL = theBaseUrl;
} else if (process.env.NODE_ENV == 'production') {
  Axios.defaults.baseURL = theBaseUrl;
}
//内网
// if (process.env.NODE_ENV == 'development') {
//   Axios.defaults.baseURL = 'http://192.168.3.244:9999'; //192.168.1.239  120.27.21.149 192.168.3.244
// } else if (process.env.NODE_ENV == 'debug') {
//   Axios.defaults.baseURL = 'http://192.168.3.244:9999';
// } else if (process.env.NODE_ENV == 'production') {
//   Axios.defaults.baseURL = 'http://192.168.3.244:9999';
// }

// Axios.defaults.headers.post['X-Requested-With'] = 'XMLHttpRequest';
// Axios.defaults.headers.get['X-Requested-With'] = 'XMLHttpRequest';
// Axios.defaults.transformRequest = [function(data) {
//   //数据序列化
//   return qs.stringify(data);
// }];

Axios.interceptors.request.use(
  config => {
    // if (localStorage.getItem('auth')) {
    //   // console.log(localStorage.getItem('token'));
    //   config.headers['auth'] = localStorage.getItem('auth')
    // }
    // console.log('this is axios config>>>', config)
    return config;
  },
  error => {
    //error的回调信息，可定义
    console.error(error);
    return Promise.reject(error);
  }
);

//返回状态判断(添加响应拦截器)
Axios.interceptors.response.use(
  response => {
    // console.log('----->', response)
    const res = response;
    return Promise.resolve(res.data);
  },
  error => {
    // console.log('err', error) //for debug
    // Message.error('网络连接错误')
    return Promise.reject(error)
  }
)

export default Axios